package xyz.funnygo.activity;

import xyz.funnygo.activity.ui.MainTab;
import xyz.funnygo.app.AppConfig;
import xyz.funnygo.app.AppManager;
import xyz.funnygo.common.Constants;
import xyz.funnygo.interf.BaseViewInterface;
import xyz.funnygo.model.User;
import xyz.funnygo.widget.MyFragmentTabHost;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements OnTabChangeListener,
		BaseViewInterface, View.OnClickListener, OnTouchListener {

	private MyFragmentTabHost mTabHost;
	
	private User mUser;
	
	private long backClickTime = System.currentTimeMillis();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bmob.initialize(getApplicationContext(), Constants.APP_ID);
		setContentView(R.layout.acitivity_main);
		initView();
		AppManager.getInstance().addActivity(this);

		handleIntent(getIntent());
		
		mUser = User.getCurrentUser(this, User.class);
        if (mUser == null) {
        	startActivity(new Intent(this, LoginActivity.class));
		}
	}

	private void handleIntent(Intent intent) {
		// TODO Auto-generated method stub

	}

	@Override
	public void initView() {
		mTabHost = (MyFragmentTabHost) findViewById(R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

		initTabs();

		mTabHost.setCurrentTab(2);
		mTabHost.setOnTabChangedListener(this);
		
		// IntentFilter filter = new
		// IntentFilter(Constants.INTENT_ACTION_NOTICE);
		// filter.addAction(Constants.INTENT_ACTION_LOGOUT);
		// registerReceiver(mReceiver, filter);
		// NoticeUtils.bindToService(this);
		//
		// if (AppContext.isFristStart()) {
		// mNavigationDrawerFragment.openDrawerMenu();
		// DataCleanManager.cleanInternalCache(AppContext.getInstance());
		// AppContext.setFristStart(false);
		// }
		//
		// checkUpdate();

	}

	private void initTabs() {
		MainTab[] tabs = MainTab.values();
		final int size = tabs.length;
		for (int i = 0; i < size; i++) {
			MainTab mainTab = tabs[i];
			TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName()));
			
			View view = LayoutInflater.from(getApplicationContext())
					.inflate(R.layout.tab_item_view, null);
			ImageView imageView = (ImageView) view.findViewById(R.id.imageview);  
	        imageView.setImageResource(mainTab.getResIcon());  
	          
	        TextView textView = (TextView) view.findViewById(R.id.textview);          
	        textView.setText(mainTab.getResName());  
	        tab.setIndicator(view);
	        
			mTabHost.addTab(tab, mainTab.getClz(), null);
			mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE);
		}

	}

	public void logout(View v) {
		User.logOut(this);
		startActivity(new Intent(this, LoginActivity.class));
		finish();
	}

	/**
     * 监听返回--是否退出程序
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        	long time = System.currentTimeMillis() - backClickTime;
        	if (time < 2000) {
				AppManager.getInstance().finishAllActivity();
			} else {
				Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
				backClickTime = System.currentTimeMillis();
				return true;
			}
        }
        return super.onKeyDown(keyCode, event);
    }
    
	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabChanged(String tabId) {
		final int size = mTabHost.getTabWidget().getTabCount();
		for (int i = 0; i < size; i++) {
			View v = mTabHost.getTabWidget().getChildAt(i);
			if (i == mTabHost.getCurrentTab()) {
				v.setSelected(true);
			} else {
				v.setSelected(false);
			}
		}
	}
	
	public User getUser(){
		return mUser;
	}
		
}
